package com.centerm.structure.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.centerm.common.constant.error.GlobalErrorEnum;
import com.centerm.common.exception.MyRuntimeException;
import com.centerm.common.view.page.PageQueryResult;
import com.centerm.structure.dao.RoleDao;
import com.centerm.structure.dao.RolePermissionDao;
import com.centerm.structure.dao.UserRoleDao;
import com.centerm.structure.domain.Role;
import com.centerm.structure.dto.RoleInfo;
import com.centerm.structure.dto.UserRoleInfo;
import com.centerm.structure.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
* @ClassName: RoleServiceImpl
* @Description: 角色服务实现类
* @author czy
* @date 2017年8月14日
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    

	@Override
	public PageQueryResult<RoleInfo> pageRoleInfoList(Integer pageNum,
			Integer pageSize,RoleInfo roleInfo) {
		Page<RoleInfo> page = PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleDao.listByParam(roleInfo);
        PageQueryResult<RoleInfo> resultInfo = new PageQueryResult<RoleInfo>(page, convertArrayResponse(list));
        return resultInfo;
	}

	@Override
	public Integer insert(RoleInfo roleInfo) {
		Role role = convertRequest(roleInfo);
		//新增记录必定有效
		role.setValid(1);
		Integer result = roleDao.insert(role);
		if(result!=1){
			String errorMsg = "新增角色错误，请排查!";
			throw new MyRuntimeException(errorMsg);
		}
		return role.getId();
	}

	@Override
	public Integer update(RoleInfo roleInfo) {
		Role role = convertRequest(roleInfo);
		Integer result =  roleDao.update(role);
		if(result!=1){
			String errorMsg = "更新角色错误，请排查!";
			throw new MyRuntimeException(errorMsg);
		}
		return result;
	}
	
	@Override
	@Transactional
	public Integer delete(Integer id) {
		if(id == null){
			throw new MyRuntimeException(GlobalErrorEnum.PARAM_ERROR);
		}
		//先判断是否有用户使用该角色
		UserRoleInfo userRoleInfo = new UserRoleInfo();
		userRoleInfo.setRoleId(id);
		List<UserRoleInfo> userRoleList = userRoleDao.listByParam(userRoleInfo);
		if(userRoleList!=null&&userRoleList.size()>0){
			String errorMsg = "有用户使用该角色，故无法删除！";
			throw new MyRuntimeException(errorMsg);
		}
		//删除相关角色权限关系
		Integer result = rolePermissionDao.deleteByRoleId(id);
		if(result<1){
			String errorMsg = "删除用户角色关系失败，请排查!";
			throw new MyRuntimeException(errorMsg);
		}
		//最后正式删除角色
		result =  roleDao.deleteById(id);
		if(result!=1){
			String errorMsg = "删除角色错误，请排查!";
			throw new MyRuntimeException(errorMsg);
		}
		return result;
	}
	
	@Override
	public RoleInfo getById(Integer id) {
		if(id == null){
			throw new MyRuntimeException(GlobalErrorEnum.PARAM_ERROR);
		}
		Role role = roleDao.getById(id);
		return convertResponse(role);
	}
	
	private Role convertRequest(RoleInfo roleInfo) {
		//TODO czy 该转换方法并不正式，存在风险
		return JSON.parseObject(JSON.toJSONString(roleInfo), Role.class);
	}
	
	private RoleInfo convertResponse(Role role) {
		//TODO czy 该转换方法并不正式，存在风险
		return JSON.parseObject(JSON.toJSONString(role), RoleInfo.class);
	}
	
	private List<RoleInfo> convertArrayResponse(List<Role> rolelist) {
		//TODO czy 该转换方法并不正式，存在风险
		return JSON.parseArray(JSON.toJSONString(rolelist), RoleInfo.class);
	}
}
